package com.dbq.fast.business.main.dialog.base;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.dbq.fast.R;

import java.util.HashSet;
import java.util.Set;


public class TipBaseDialog extends DialogFragment implements View.OnClickListener {

    protected static String DIALOG_TAG = "tip_dialog";

    protected TextView tipTV;
    protected TextView op1TV, op2TV;
    protected View dividerView;

    private IOnClickListener listener;

    //管理子类对话框，避免弹已在弹出状态的对话框，造成多个对话框累叠
    public static final Set<String> childSet = new HashSet<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00ffffff));
        getDialog().setCanceledOnTouchOutside(false);

        View view = inflater.inflate(R.layout.dialog_tip, container, false);

        tipTV = (TextView) view.findViewById(R.id.tip_info);
        op1TV = (TextView) view.findViewById(R.id.op_1);
        op2TV = (TextView) view.findViewById(R.id.op_2);
        dividerView = view.findViewById(R.id.op_divider);
        op1TV.setOnClickListener(this);
        op2TV.setOnClickListener(this);

        showInfo();

        return view;
    }

    private void showInfo() {
        Bundle bundle = getArguments();
        String tipText = bundle.getString("tip_text", "");
        String op1Text = bundle.getString("op1_text", "");
        String op2Text = bundle.getString("op2_text", "");
        int op1Color = bundle.getInt("op1_color", -1);
        boolean needCustomOp = bundle.getBoolean("needCustomOp", true);

        tipTV.setText(tipText);
        op1TV.setText(op1Text);
        op2TV.setText(op2Text);
        if (op1Color != -1) {
            op1TV.setTextColor(op1Color);
        }
        op2TV.setVisibility(needCustomOp ? View.VISIBLE : View.GONE);
        dividerView.setVisibility(needCustomOp ? View.VISIBLE : View.GONE);
    }

    public TipBaseDialog show(FragmentManager fragmentManager) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        if (fragmentManager.findFragmentByTag(DIALOG_TAG) instanceof TipBaseDialog) {
            TipBaseDialog prev = (TipBaseDialog) fragmentManager.findFragmentByTag(DIALOG_TAG);
            if (prev != null) {
                if (prev.isAdded()) {
                    prev.dismiss();
                }
                ft.remove(prev);
            }
            ft.addToBackStack(null);
        }
        try {
            this.show(ft, DIALOG_TAG);
        }catch (Exception e){}
        return this;
    }

    public static class Builder {
        private String tipText;
        private String op1Text;
        private String op2Text;
        private int op1Color;
        private boolean needCustomOp;
        private TipBaseDialog dialog;

        public Builder setTipText(String text) {
            this.tipText = text;
            return this;
        }

        public Builder setOp1Text(String text) {
            this.op1Text = text;
            return this;
        }

        public Builder setOp2Text(String text) {
            this.op2Text = text;
            return this;
        }

        public Builder setOp1Color(int color) {
            this.op1Color = color;
            return this;
        }

        public Builder setNeedCustomOp(boolean needCustomOp) {
            this.needCustomOp = needCustomOp;
            return this;
        }

        public TipBaseDialog build() {
            dialog = new TipBaseDialog();
            Bundle args = new Bundle();
            args.putString("tip_text", tipText);
            args.putString("op1_text", op1Text);
            args.putString("op2_text", op2Text);
            args.putInt("op1_color", op1Color);
            args.putBoolean("needCustomOp", needCustomOp);
            dialog.setArguments(args);
            return dialog;
        }
    }

    public interface IOnClickListener {
        void operate1Click();

        void operate2Click();
    }

    public void setOnClickListener(IOnClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.op_1:
                if (listener != null) {
                    listener.operate1Click();
                }
                break;
            case R.id.op_2:
                if (listener != null) {
                    listener.operate2Click();
                }
                break;
        }
        exitMap(DIALOG_TAG);
        dismiss();
    }

    @Override
    public void onDestroy() {
        exitMap(DIALOG_TAG);
        super.onDestroy();
    }

    /**
     * 子类管理 --- start
     */

    //当前子类对话框是否已显示
    public static boolean isShowing(String key){
        return childSet.contains(key);
    }

    //子类对话框加入显示集合
    public static void joinMap(String key){
        childSet.add(key);
    }

    //子类对话框从显示集合中移除
    public static void exitMap(String key){
        childSet.remove(key);
    }

    /**
     * 子类管理 --- end
     */

    @Override
    public void onSaveInstanceState(Bundle outState) {

    }
}
